Boolean literal and parameter handling in object relational mapping

ABSTRACT

Techniques for object relational mapping in database technologies are described herein. According to one embodiment, in response to a query statement for accessing a relational database, a syntax tree is generated to represent semantic information of the query statement, where the query statement has a boolean parameter and is implemented as an SQL object. A data type of the boolean parameter is predicted based on the semantic information obtained from the syntax tree in view of a structure representing the syntax tree. The boolean parameter is configured to be either a numeric value or a string dependent upon metadata used to map the SQL object to the relational database. Other methods and apparatuses are also described.

TECHNICAL FIELD

The present invention relates generally to object relational mapping in database management. More particularly, this invention relates to improved boolean literal and parameter handling in object relational mapping

BACKGROUND

Data management tasks in object-oriented programming are typically implemented by manipulating objects, which are typically non-scalar values. Consider the example of an address book entry, which represents a single person along with zero or more phone numbers and zero or more addresses. This could be modeled in an object-oriented implementation by a “person object” with “slots” to hold the data that comprise the entry: the person's name, a list (or array) of phone numbers, and a list of addresses. The list of phone numbers would itself contain “phone number objects” and so on. The address book entry is treated as a single value by the programming language (it can be referenced by a single variable, for instance). Various methods can be associated with the object, such as a method to return the preferred phone number, the home address, and so on.

Many popular database products, however, such as SQL DBMS products, can only store and manipulate scalar values such as integers and strings, organized within tables. A programmer must either convert the object values into groups of simpler values for storage in the database (and convert them back upon retrieval), or only use simple scalar values within the program. Object-relational mapping is used to implement the first approach.

Object-relational mapping (aka O/RM, ORM, and O/R mapping) is a programming technique for converting data between incompatible type systems in databases and object-oriented programming languages. This creates, in effect, a “virtual object database” which can be used from within the programming language.

Typically, a conventional ORM system provides an application programming interface (API) to allow a user to specifically set a data type of a parameter during mapping an object with a database entry. If a user enters a wildcard parameter such as “?” or “*”, the conventional ORM is not able to determine the data type. Further, a boolean entry of an underlying database may be represented as a numeric value or a string. When a user sets a boolean value, the user has to specifically specify whether the boolean value is in a form of a numeric value or a string value. Otherwise, the conventional ORM system cannot handle the data type for the boolean parameter.

BRIEF DESCRIPTION OF THE DRAWINGS

The present invention is illustrated by way of example and not limitation in the figures of the accompanying drawings in which like references indicate similar elements.

FIG. 1 is a block diagram illustrating an application framework which may be used with one embodiment of the invention.

FIG. 2 is a block diagram illustrating an application framework according to an alternative embodiment of the invention.

FIG. 3 is pseudo code representing an example of metadata for object relational mapping.

FIG. 4 is a diagram illustrating a process performed by a query analyzer according to one embodiment of the invention.

FIG. 5 is a flow diagram illustrating a process for predicting parameter type based on a syntax tree according to one embodiment.

FIG. 6 is a diagram illustrating a process performed by a query analyzer according to one embodiment of the invention.

FIG. 7 is a flow diagram illustrating a process for predicting parameter type based on a syntax tree according to one embodiment.

DETAILED DESCRIPTION

In the following description, numerous details are set forth to provide a more thorough explanation of embodiments of the present invention. It will be apparent, however, to one skilled in the art, that embodiments of the present invention may be practiced without these specific details. In other instances, well-known structures and devices are shown in block diagram form, rather than in detail, in order to avoid obscuring embodiments of the present invention.

Reference in the specification to “one embodiment” or “an embodiment” means that a particular feature, structure, or characteristic described in connection with the embodiment is included in at least one embodiment of the invention. The appearances of the phrase “in one embodiment” in various places in the specification do not necessarily all refer to the same embodiment.

FIG. 1 is a block diagram illustrating an application framework which may be used with one embodiment of the invention. Referring to FIG. 1, framework 100 includes, but is not limited to, an application client 101 accessing a database 103 through a common query API (application programming interface) 102 with object-relational mapping (ORM) features, also referred to as an ORM module. The API 102 provides the ability to map an object model's data representation to a relational data model and its corresponding database schema. For example, in a Java computing environment, the API 102 takes care of the mapping from Java classes to database tables and from Java data types to SQL (structured query language) data types used in a database. Note that throughout this application and for the purposes of illustration only, Java is used as an example of a cross-platform programming language. However, it is not so limited; other cross-platform programming languages may also be applied. The API 102 provides data query and retrieval facilities that reduce development time, which is designed to relieve a software developer from most of common data persistence-related programming tasks by reducing the need for manual, hand-crafted data processing using SQL and JDBC (Java database connectivity). The JDBC API is the industry standard for database-independent connectivity between the Java programming language and a wide range of databases—SQL databases and other tabular data sources, such as spreadsheets or flat files.

According to one embodiment, ORM 102 includes a query analyzer 104 and a data type predictor 105. The query analyzer 104, in response to a query, performs an analysis on the query to generate a syntax tree representing semantic information with respect to the query. Based on the syntax tree, the data type predictor 105 is configured to predict or guess the data type of certain parameters of the query. This is done without the application to specifically set the data type of the parameters. In a particular embodiment, data type predictor 105 may predict the data type of a Boolean parameter either in a numeric format or a string format dependent upon the requirement of the underlying database being accessed. In one embodiment, the data type predictor 105 may predict the data type based on metadata 106 that defines certain rules of mapping a specific object oriented programming object to a specific SQL of a particular database. An example of metadata is shown in FIG. 3 in an XML (extensible markup language) format.

In one embodiment, the query analyzer 104 includes a parser or translator generated using ANTLR (ANother Tool for Language Recognition) technologies. ANTLR allows one to define language grammars in either ANTLR syntax or a specific AST (abstract syntax tree) syntax. Further detailed information concerning ANTLR project can be found at ANTLR.ORG.

FIG. 2 is a block diagram illustrating an application framework according to an alternative embodiment of the invention. Referring to FIG. 2, similar to framework 100 as shown in FIG. 1, framework 150 includes, but is not limited to, an application 151 accessing database 153 via an application framework 152 which may be implemented as part of framework 102 of FIG. 1. In this example, framework 152 is configured to provide persistent services to application 151.

Framework 152 includes a variety of components. For example, session factory 154 is configured to provide a thread-safe (immutable) cache of compiled mappings for a single database. Session factory 154 is a factory for session 155 and a client of connection provider 158 and it may hold an optional (e.g., second-level) cache of data that is reusable between transactions, at a process- or cluster-level. Session 155 is a single-threaded, short-lived object representing a conversation between the application 151 and the persistent store 153. Session 155 wraps a JDBC connection and is a factory for transaction 157. Session 155 holds a mandatory (first-level) cache of persistent objects, which are used when navigating an object graph or looking up objects based on an identifier.

Persistent objects 156 are short-lived, single threaded objects containing persistent state and business function. Persistent objects 156 may be ordinary JavaBeans/POJOs (plain old Java objects). Persistent objects 156 may be associated with a session (e.g., session 155). As soon as the associated session is closed, the persistent objects 156 will be detached and free to use in any application layer (e.g. directly as data transfer objects to and from presentation). Transaction 157 is a single-threaded, short-lived object used by the application to specify atomic units of work. Transaction 157 abstract applications from underlying JDBC (Java database connectivity), JTA (Java transaction API), and/or CORBA (common object request broker architecture) transactions. A session may span several transactions in some situations.

The JDBC API is the industry standard for database-independent connectivity between the Java programming language and a wide range of databases—SQL databases and other tabular data sources, such as spreadsheets or flat files. The JDBC API provides a call-level API for SQL-based database access. The JTA specifies standard Java interfaces between a transaction manager and the parties involved in a distributed transaction system, such as the resource manager, the application server, and the transactional applications. The CORBA is a standard defined by the Object Management Group (OMG) that enables software components written in multiple computer languages and running on multiple computers to interoperate. CORBA “wraps” program code into a bundle containing information about the capabilities of the code inside and how to call it. The resulting wrapped objects can then be called from other programs (or CORBA objects) across a network. CORBA uses an interface definition language (IDL) to specify the interfaces that objects will present to the world. CORBA then specifies a “mapping” from IDL to a specific implementation language like C++ or Java.

Referring back to FIG. 2, connection provider 158 is a factory for (and pool of) JDBC connections. Connection provider 158 abstracts application 151 from underlying data sources or driver managers (e.g., JNDI or Java naming and directory interface 160, JDBC 161, and JTA 162). Transaction factory 159 is a factory for transaction 157 instances. Framework 150 supports a variety of databases, including, for example, Oracle, DB2, Sybase, MS SQL server, MySQL, etc. In one embodiment, framework 150 further includes a query analyzer and a data type predictor as described above with respect to FIG. 1. Note that some or all of the components as shown in FIGS. 1-2 may be implemented in hardware, software, or a combination of both hardware and software.

FIG. 4 is a diagram illustrating a process performed by a query analyzer according to one embodiment of the invention. Given a query statement or string 401, a conventional method is to call a specific method to set the parameter in which case the ORM unit interprets the parameter type. For example, the conventional method would have to use Query.setParameter( ) method to set a particular parameter in which the ORM interprets the type of the parameter.

According to one embodiment, referring to FIG. 4, given a query statement 401, a query analysis is performed by a query analyzer, which may be generated using ANTLR techniques. As a result, a syntax tree structure 402 is generated, where the syntax tree 402 includes semantic information about the query statement 401. Based on the semantic information obtained from the syntax tree 402, the ORM unit can predict the missing data type of the parameter as shown in 403 and fill in the necessary information before accessing the underlying database.

In this example, given a query 401 of “from Person a where a.name=?” and the syntax tree 402, although the query statement does not provide the parameter type by only giving a wildcard “?”, the ORM unit can predict that the parameter type is most likely the same as the underlying type of “Person.name” property which defines the object or class of “a”, where the predicted parameter type may be a string as shown in syntax 403.

FIG. 5 is a flow diagram illustrating a process for predicting parameter type based on a syntax tree according to one embodiment. Note that process 500 may be performed by processing logic which may include software, hardware, or a combination of both. For example, process 500 may be performed by system 100 as shown in FIG. 1. Referring to FIG. 5, at block 501, processing logic receives a query statement having a wildcard parameter. In response, at block 502, processing logic invokes a query translator or analyzer to interpret the query statement. At block 503, processing logic generates a syntax tree having multiple nodes representing semantic information of the query statement. Based on the syntax tree, at block 504, for a given node, processing logic predicts a parameter type associated with the node based on a property of the node. Other operations may also be performed.

In addition, the above techniques may also be used in certain situations to predict the data type of a Boolean parameter. For example, a Boolean value in a database technology may be represented as a numeric value such as a zero or non-zero value. However, a Boolean value may also be represented via a string such as “true” or “false”, or other strings such as “yes” or “no”. Whether a Boolean value is represented via a numeric number of a specific string depends on a specific implementation of the underlying database.

Conventionally, Boolean literals are used as an actual SQL construct, which allows a user to do a query as follows:

List pregs=session.createQuery (“from Animal a where a.pregnant=true”).list( ) However, these are typically resolved to SQL literal values based on the dialect and are thus non-contextually resolved, for example, using a method similar to Dialet.toBooleanValueString( ). Using techniques similar to those described above, according to one embodiment, the Boolean literals may be resolved contextually. In the above example, the TRUE literal would be resolved to a SQL literal using the type associated with Animal.pregnent property.

FIG. 6 is a diagram illustrating a process performed by a query analyzer according to one embodiment of the invention. Given a query statement or string 601, a conventional method is to call a specific method to set the parameter in which case the ORM unit interprets the parameter type. For example, the conventional method would have to use Dialet.toBooleanValueString( ) method to set a Boolan parameter type in which the ORM interprets the type of the parameter.

According to one embodiment, referring to FIG. 6, given a query statement 601, a query analysis is performed by a query analyzer, which may be generated using ANTLR techniques. As a result, a syntax tree structure 602 is generated, where the syntax tree 602 includes semantic information about the query statement 601. Based on the semantic information obtained from the syntax tree 602, the ORM unit can predict the missing Boolean data type of the parameter as shown in 603 and fill in the necessary information before accessing the underlying database.

In this example, given a query 601 of “from Animal a where a.pregnent=true” and the syntax tree 602, the ORM unit can predict that the parameter type is most like the same as the underlying type of “Animal.pregnant” property which defines the object or class of “a”, where the predicted parameter type may be a string as shown in syntax 603. The type (e.g., either a numeric value or a string) used by the underlying database may be determined from the metadata, such as metadata as shown in FIG. 3, that maps the query to the specific SQL of the underlying database.

FIG. 7 is a flow diagram illustrating a process for predicting parameter type based on a syntax tree according to one embodiment. Note that process 700 may be performed by processing logic which may include software, hardware, or a combination of both. For example, process 700 may be performed by system 100 as shown in FIG. 1. Referring to FIG. 7, at block 701, processing logic receives a query statement having a boolean parameter. In response, at block 702, processing logic invokes a query translator or analyzer to interpret the query statement. At block 703, processing logic generates a syntax tree having multiple nodes representing semantic information of the query statement. Based on the syntax tree, at block 704, for each node having a boolean value, processing logic examines the metadata associated with the node to determine a data type of the node. Based on the determined data type, at block 705, processing logic configures a proper boolean value either in a string or numeric value corresponding to a underlying database. For example, if a query has a boolean value of “true”, after the analysis, the boolean value may be predicted and translated to a numeric value of “1” or another string of “yes”, which corresponds to the data type of the underlying database. Other operations may also be performed.

Some portions of the preceding detailed descriptions have been presented in terms of algorithms and symbolic representations of operations on data bits within a computer memory. These algorithmic descriptions and representations are the ways used by those skilled in the data processing arts to most effectively convey the substance of their work to others skilled in the art. An algorithm is here, and generally, conceived to be a self-consistent sequence of operations leading to a desired result. The operations are those requiring physical manipulations of physical quantities. Usually, though not necessarily, these quantities take the form of electrical or magnetic signals capable of being stored, transferred, combined, compared, and otherwise manipulated. It has proven convenient at times, principally for reasons of common usage, to refer to these signals as bits, values, elements, symbols, characters, terms, numbers, or the like.

It should be borne in mind, however, that all of these and similar terms are to be associated with the appropriate physical quantities and are merely convenient labels applied to these quantities. Unless specifically stated otherwise as apparent from the above discussion, it is appreciated that throughout the description, discussions utilizing terms such as “processing” or “computing” or “calculating” or “determining” or “displaying” or the like, refer to the action and processes of a computer system, or similar electronic computing device, that manipulates and transforms data represented as physical (electronic) quantities within the computer system's registers and memories into other data similarly represented as physical quantities within the computer system memories or registers or other such information storage, transmission or display devices.

Embodiments of the present invention also relate to an apparatus for performing the operations herein. This apparatus may be specially constructed for the required purposes, or it may comprise a general-purpose computer selectively activated or reconfigured by a computer program stored in the computer. Such a computer program may be stored in a computer readable storage medium, such as, but is not limited to, any type of disk including floppy disks, optical disks, CD-ROMs, and magnetic-optical disks, read-only memories (ROMs), random access memories (RAMs), erasable programmable ROMs (EPROMs), electrically erasable programmable ROMs (EEPROMs), magnetic or optical cards, or any type of media suitable for storing electronic instructions, and each coupled to a computer system bus.

The algorithms and displays presented herein are not inherently related to any particular computer or other apparatus. Various general-purpose systems may be used with programs in accordance with the teachings herein, or it may prove convenient to construct more specialized apparatus to perform the required method operations. The required structure for a variety of these systems will appear from the description below. In addition, embodiments of the present invention are not described with reference to any particular programming language. It will be appreciated that a variety of programming languages may be used to implement the teachings of embodiments of the invention as described herein.

A machine-readable medium may include any mechanism for storing or transmitting information in a form readable by a machine (e.g., a computer). For example, a machine-readable medium includes read only memory (“ROM”); random access memory (“RAM”); magnetic disk storage media; optical storage media; flash memory devices; electrical, optical, acoustical or other form of propagated signals (e.g., carrier waves, infrared signals, digital signals, etc.); etc.

In the foregoing specification, embodiments of the invention have been described with reference to specific exemplary embodiments thereof. It will be evident that various modifications may be made thereto without departing from the broader spirit and scope of the invention as set forth in the following claims. The specification and drawings are, accordingly, to be regarded in an illustrative sense rather than a restrictive sense. 

1. A computer implemented method, comprising: in response to a query statement for accessing a relational database, generating, by a query analyzer, a syntax tree representing semantic information of the query statement, the query statement having a boolean parameter without specifying a data type of the boolean parameter; predicting, by a data type predictor, a data type of the boolean parameter based on the semantic information obtained from the syntax tree in view of a structure representing the syntax tree, wherein the boolean parameter is configured to be either a numeric value or a string dependent upon metadata that defines one or more rules to map a query statement to a structured query language (SQL) statement specifically tailored to a format of the relational database; generating, by an object-relational mapping (ORM) unit, an SQL statement corresponding to the format of the relational database based on the metadata, the SQL statement having a parameter representing the boolean parameter of the query statement in one of a numeric value and a string formats based on the predicted data type; and accessing an entry of the relational database using the SQL statement.
 2. The computer implemented method of claim 1, wherein the metadata is in an extensible markup language (XML) format.
 3. The computer implemented method of claim 2, further comprising examining the metadata to obtain a corresponding data type of a data entry of the relational database corresponding to the query statement to determine a proper value.
 4. The computer implemented method of claim 3, wherein a data type of the metadata indicates that a boolean type of the data entry is either in a numeric format or a string format.
 5. The computer implemented method of claim 4, further comprising translating the boolean parameter from a string format provided from the query statement into one of the numeric format and string format based on information obtained from the metadata.
 6. The computer implemented method of claim 5, wherein the metadata is provided previously from a user designing mapping between an object and one or more data entries of the relational database.
 7. A machine-readable storage medium having instructions stored therein, which when executed by a processor, cause the processor to perform a method, the method comprising: in response to a query statement for accessing a relational database, generating, by a query analyzer, a syntax tree representing semantic information of the query statement, the query statement having a boolean parameter without specifying a data type of the boolean parameter; and predicting, by a data type predictor, a data type of the boolean parameter based on the semantic information obtained from the syntax tree in view of a structure representing the syntax tree, wherein the boolean parameter is configured to be either a numeric value or a string dependent upon metadata that defines one or more rules to map a query statement to a structured query language (SQL) statement specifically tailored to a format of the relational database; generating, by an object-relational mapping (ORM) unit, an SQL statement corresponding to the format of the relational database based on the metadata, the SQL statement having a parameter representing the boolean parameter of the query statement in one of a numeric value and a string formats based on the predicted data type; and accessing an entry of the relational database using the SQL statement.
 8. The machine-readable storage medium of claim 7, wherein the method further comprises mapping an SQL object with the predicted data type of the boolean parameter to a corresponding data entry of the relational database.
 9. The machine-readable storage medium of claim 8, wherein the method further comprises examining the metadata to obtain a corresponding data type of a data entry of the relational database corresponding to the query statement to determine a proper value.
 10. The machine-readable storage medium of claim 9, wherein a data type of the metadata indicates that a boolean type of the data entry is either in a numeric format or a string format.
 11. The machine-readable storage medium of claim 10, wherein the method further comprises translating the boolean parameter from a string format provided from the query statement into one of the numeric format and string format based on information obtained from the metadata.
 12. The machine-readable storage medium of claim 11, wherein the metadata is provided previously from a user designing mapping between an object and one or more data entries of the relational database.
 13. A data processing system, comprising: a processor; and a memory coupled to the processor, the memory including executed therein, a query analyzer, in response to a query statement for accessing a relational database, to generate a syntax tree representing semantic information of the query statement, the query statement having a boolean parameter without specifying a data type of the boolean parameter; a data type predictor coupled to the query analyzer to predict a data type of the boolean parameter based on the semantic information obtained from the syntax tree in view of a structure representing the syntax tree, wherein the boolean parameter is configured to be either a numeric value or a string dependent upon metadata that defines one or more rules to map a query statement to a structured query language (SQL) statement specifically tailored to a format of the relational database; and an object-relational mapping (ORM) unit coupled to the query analyzer and data type predictor to generate an SQL statement corresponding to the format of the relational database based on the metadata, the SQL statement having a parameter representing the boolean parameter of the query statement in one of a numeric value and a string formats based on the predicted data type, and wherein an entry of the relational database is accessed using the SQL statement.
 14. The data processing system of claim 13, wherein the metadata is in an extensible markup language (XML) format.
 15. The data processing system of claim 14, wherein the query analyzer is configured to examine the metadata to obtain a corresponding data type of a data entry of the relational database corresponding to the query statement to determine a proper value.
 16. The data processing system of claim 15, wherein a data type of the metadata indicates that a boolean type of the data entry is either in a numeric format or a string format.
 17. The data processing system of claim 16, further comprising a query translator to translate the boolean parameter from a string format provided from the query statement into one of the numeric format and string format based on information obtained from the metadata.
 18. The data processing system of claim 17, wherein the metadata is provided previously from a user designing mapping between an object and one or more data entries of the relational database. 